//
//  WJHtmlView.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJHtmlView.h"
#import "WJEssAndNewModel.h"
#import "DALabeledCircularProgressView.h"
#import "WJShowHtmlVC.h"
#import "WJDataManager.h"

@interface WJHtmlView ()

// 下面的图片
@property (nonatomic,strong) WJBaseImageView *huiSeDeImage;
// 上面写"点击查看全图"
@property (nonatomic,strong) UILabel *huiseLable;

@property (nonatomic, strong) DALabeledCircularProgressView *progressView;

@end

@implementation WJHtmlView

-(void)dealloc
{
    [_model release];
    [_huiseLable release];
    [_huiSeDeImage release];
    [_progressView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
    }
    return self;
}


- (void)createView
{
    
    // 这个是圆圈圈
   self.progressView = [[DALabeledCircularProgressView alloc] init];
    self.progressView.progressTintColor = [UIColor orangeColor];
    // 头变成圆的
    self.progressView.roundedCorners = YES;
    self.progressView.hidden = YES;
    [self addSubview:self.progressView];
    [_progressView release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];                 // 给视图添加手势  添加手指
    [tap release];
    
    self.huiSeDeImage = [[WJBaseImageView alloc]init];
    [self addSubview:self.huiSeDeImage];
    self.huiSeDeImage.image = [UIImage imageNamed:@"see-big-picture-background"];
    self.huiSeDeImage.backgroundColor = [UIColor clearColor];
    [_huiSeDeImage release];
    
    
    self.huiseLable = [[UILabel alloc]init];
    [self.huiSeDeImage addSubview:self.huiseLable];
    self.huiseLable.backgroundColor = [UIColor clearColor];
    self.huiseLable.textColor = [UIColor whiteColor];
    self.huiseLable.text = @"查看全文";
    self.huiseLable.textAlignment = 1;
    [_huiseLable release];
    
}





- (void)setModel:(WJEssAndNewModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    [self sd_setImageWithURL:[NSURL URLWithString:self.model.htmlModel.thumbnail[0]] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        if ([[WJDataManager shareFMDB] isExistPicName:self.model.htmlModel.thumbnail[0]]) {
            
            self.progressView.hidden = YES;
            
        }else{

        self.progressView.hidden = NO;
        
        self.huiSeDeImage.hidden = YES;
        
        self.huiseLable.hidden = YES;
        
        CGFloat proges = 1.0 * receivedSize / expectedSize;
        
        // 显示文字
        NSString *text = [NSString stringWithFormat:@"%.0f%%", proges * 100];
        self.progressView.progressLabel.text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
        
        //显示进度值
        [self.progressView setProgress:proges animated:NO];
            
        }
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.progressView.hidden = YES;
        
        self.huiseLable.hidden = NO;
        
        self.huiSeDeImage.hidden = NO;
        
        if (![[WJDataManager shareFMDB] isExistPicName:self.model.htmlModel.thumbnail[0]]) {
            
            [[WJDataManager shareFMDB] insertPicName:self.model.htmlModel.thumbnail[0]];
            
        }
    }];
    
    self.progressView.frame = CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60);
   
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.huiSeDeImage.frame = CGRectMake(-10,  self.bounds.size.height - 40 , WIDTH - 60, 40);
    
    self.huiseLable.frame = CGRectMake(0, 10, WIDTH - 60 , 15);
    
    self.progressView.frame = CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60);

}





#pragma mark - 点击可以进入另一个界面
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    WJShowHtmlVC *vc = [[WJShowHtmlVC alloc]init];
    
    vc.model = self.model;
    
    // 主屏显示
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
    
     NSLog(@"点了HTML");
    
}



@end
